package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication.db.DBManager;
import com.example.myapplication.model.Course;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CourseListActivity extends AppCompatActivity {
    private ListView listView;
    private ArrayList<Course> courseList;
    private String teacherName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_list);

        // 设置ActionBar
        if (getSupportActionBar() != null) {
            String teacherName = getIntent().getStringExtra("teacherName");
            getSupportActionBar().setTitle(teacherName + "的课程");
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setElevation(4);
        }

        teacherName = getIntent().getStringExtra("teacherName");
        if (teacherName == null || teacherName.isEmpty()) {
            Toast.makeText(this, "参数错误", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 设置标题
        setTitle(teacherName + "的课程");
        TextView titleText = findViewById(R.id.titleText);
        if (titleText != null) {
            titleText.setText(teacherName + "的课程");
        }

        // 显示加载进度
        View loadingView = findViewById(R.id.loadingProgress);
        if (loadingView != null) {
            loadingView.setVisibility(View.VISIBLE);
        }

        // 获取课程列表
        new Thread(() -> {
            try {
                DBManager dbManager = new DBManager(this);
                courseList = dbManager.getCoursesByTeacherName(teacherName);

                // 在主线程更新UI
                runOnUiThread(() -> {
                    try {
                        if (loadingView != null) {
                            loadingView.setVisibility(View.GONE);
                        } // 如果没有数据，显示提示并返回
                        if (courseList.isEmpty()) {
                            Toast.makeText(this, "没有找到相关课程", Toast.LENGTH_SHORT).show();
                            finish();
                            return;
                        }

                        // 设置适配器
                        CourseAdapter adapter = new CourseAdapter(this, courseList);

                        listView = findViewById(R.id.courseList);
                        if (listView != null) {
                            listView.setAdapter(adapter);

                            // 设置点击事件
                            listView.setOnItemClickListener((parent, view, position, id) -> {
                                try {
                                    Intent intent = new Intent(CourseListActivity.this, CourseDetailActivity.class);
                                    intent.putExtra("courseName", courseList.get(position).getName());
                                    intent.putExtra("teacherName", teacherName);
                                    startActivity(intent);
                                } catch (Exception e) {
                                    Toast.makeText(this, "打开课程详情失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    } catch (Exception e) {
                        Toast.makeText(this, "显示数据失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        finish();
                    }
                });
            } catch (Exception e) {
                runOnUiThread(() -> {
                    if (loadingView != null) {
                        loadingView.setVisibility(View.GONE);
                    }
                    Toast.makeText(this, "获取数据失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    finish();
                });
            }
        }).start();
    }

    @Override
    public boolean onOptionsItemSelected(android.view.MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
